package org.taobao.ibatiside.ui.wizard;

import java.beans.PropertyChangeEvent;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.datatools.modelbase.sql.query.QueryStatement;
import org.eclipse.emf.databinding.EMFDataBindingContext;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.taobao.ibatiside.model.mapperv2.DocumentRoot;
import org.taobao.ibatiside.ui.util.Mapper2XMLProcessor;
import org.taobao.ibatiside.ui.util.MiscHelper;

public class StatementPage extends BatisCommonPage {
	StatementGeneralComposite statementGeneralComposite;
	StatementParameterComposite statementParameterComposite;
	StatementContentComposite statementContentComposite;
	StatementResultComposite statementResultComposite;

	public StatementPage(String pageName) {
		super(pageName);
		setDescription("All settings for a valid iBATIS statement");
		setTitle("Statement");
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		super.propertyChange(evt);
		if (BatisSettingModel.K_DTP_STMT.equals(evt.getPropertyName())) {
			// create new statement against this statement
			QueryStatement old = (QueryStatement) evt.getOldValue();
			QueryStatement stmt = (QueryStatement) evt.getNewValue();
			if (MiscHelper.needCreateNewEmfStatement(old, stmt)) {
				model.setStatement(MiscHelper
						.createEMFStatementByDtpStatement(stmt));
			}
		} else if (BatisSettingModel.K_SQLMAP_FILE
				.equals(evt.getPropertyName())) {
			loadSqlMapModelFromFile((IFile) evt.getNewValue());
		}
	}

	private void loadSqlMapModelFromFile(IFile iFile) {
		if (iFile != null && iFile.isAccessible()) {
			Mapper2XMLProcessor mapper_processor = new Mapper2XMLProcessor();
			try {
				Resource emf_resource = mapper_processor.load(
						iFile.getContents(), null);
				model.setSqlmap(((DocumentRoot) emf_resource.getContents().get(
						0)).getSqlMap());
			} catch (Exception e) {
				setErrorMessage("error when loading: "
						+ e.getLocalizedMessage());
			}
		} else {
			setErrorMessage("can't load sqlmap from" + iFile.toString());
		}
	}

	@Override
	public boolean isPageComplete() {
		//basic
		boolean completed =  model.getSqlmap() != null && model.getStatement() != null
				&& StringUtils.isNotBlank(model.getStatement().getId()) ;
		//parameter;
		if(model.getParameterRequired()){
			if (model.getParameterUseClass()){
				completed = completed && model.getParameterClass() != null;
			}else{
				completed = completed && model.getParameterMap() != null;
			}
		}
		//result
		if (model.getResultRequired()){
			if (model.getResultUseClass()){
				completed = completed && model.getResultClass() != null;
			}else{
				completed = completed && model.getResultMap() != null;
			}
		}
		return completed;
	}

	@Override
	public IWizardPage getNextPage() {
		if (model.getDaoPageRequired()) {
			return super.getNextPage();
		}
		return null;
	}

	@Override
	public void doCreateControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		setControl(container);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));

		TabFolder tabFolder = new TabFolder(container, SWT.NONE);

		TabItem tbtmBasic = new TabItem(tabFolder, SWT.NONE);
		tbtmBasic.setText("General");

		statementGeneralComposite = new StatementGeneralComposite(tabFolder,
				SWT.NONE, this);
		tbtmBasic.setControl(statementGeneralComposite);

		TabItem tbtmParameter = new TabItem(tabFolder, SWT.NONE);
		tbtmParameter.setText("Parameter");

		statementParameterComposite = new StatementParameterComposite(
				tabFolder, SWT.NONE, this);
		tbtmParameter.setControl(statementParameterComposite);

		TabItem tbtmContent = new TabItem(tabFolder, SWT.NONE);
		tbtmContent.setText("Content");

		statementContentComposite = new StatementContentComposite(tabFolder,
				SWT.NONE, this);
		tbtmContent.setControl(statementContentComposite);
		new Label(statementContentComposite, SWT.NONE);
		new Label(statementContentComposite, SWT.NONE);

		TabItem tbtmResult = new TabItem(tabFolder, SWT.NONE);
		tbtmResult.setText("Result");

		statementResultComposite = new StatementResultComposite(tabFolder,
				SWT.NONE, this);
		tbtmResult.setControl(statementResultComposite);
	}

	@Override
	protected void doBinding(EMFDataBindingContext bindingContext) {

	}
}
